package com.chunfeng.service.impl;

import com.chunfeng.entity.Orders;
import com.chunfeng.mapper.OrdersMapper;
import com.chunfeng.result.JsonRequest;
import com.chunfeng.result.ServiceEnum;
import com.chunfeng.service.IOrdersService;
import com.chunfeng.utils.SqlDateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 订单业务层实现类
 *
 * @author by 春风能解释
 * <p>
 * 2022/12/20
 */
@Service
@Transactional
@Slf4j
public class OrdersServiceImpl implements IOrdersService {
    /**
     * 获取当前时间
     */
    private final String date = SqlDateUtils.date;
    /**
     * 订单mapper注入
     */
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 查看所有订单信息
     *
     * @return JSON
     */
    @Override
    @Cacheable(value = "order")
    public JsonRequest<List<Orders>> lookAllOrders() {
        List<Orders> orders = ordersMapper.selectAllOrders();
        if (orders.isEmpty()) {
            log.warn("未查询到任何订单信息!");
            return JsonRequest.error(ServiceEnum.NOT_FOUND);
        }
        log.info("已查询到{}条订单信息!", orders.size());
        return JsonRequest.success(orders);
    }

    /**
     * 按条件获取订单信息
     *
     * @param order 订单名
     * @return JSON
     */
    @Override
    @Cacheable(value = "order", key = "#order")
    public JsonRequest<List<Orders>> lookOrdersByCondition(Orders order) {
        List<Orders> orders = ordersMapper.conditionSelectAllOrders(order);
        if (orders.isEmpty()) {
            log.warn("未查询到任何订单信息!");
            return JsonRequest.error(ServiceEnum.NOT_FOUND);
        }
        log.info("已查询到{}条订单信息!", orders.size());
        return JsonRequest.success(orders);
    }


    /**
     * 新增一条订单信息
     *
     * @param orders 订单信息
     * @return JSON
     */
    @Override
    @CacheEvict(value = "order", allEntries = true)
    public JsonRequest<Integer> insertOneOrder(Orders orders) {
        orders.setCreateUser(SqlDateUtils.currentUserId);
        orders.setCreateTime(date);
        Integer column = ordersMapper.insertOrders(orders);
        if (column < 1) {
            log.error("订单信息添加失败!返回值:{}", column);
            return JsonRequest.error(ServiceEnum.INSERT_ERROR);
        }
        log.info("订单信息添加成功!返回值:{}", column);
        return JsonRequest.success(column);
    }

    /**
     * 修改一条订单信息
     *
     * @param orders 订单信息
     * @return JSON
     */
    @Override
    @CacheEvict(value = "order", allEntries = true)
    public JsonRequest<Integer> updateOneOrder(Orders orders) {
        Orders orders1 = ordersMapper.selectAllOrdersById(orders.getId());
        if (Objects.isNull(orders1)) {
            log.warn("未查询到ID为{}的订单信息!", orders.getId());
            return JsonRequest.error(ServiceEnum.NOT_FOUND);
        }
        //注入
        orders.setUpdateUser(SqlDateUtils.currentUserId);
        orders.setUpdateTime(date);
        Integer column = ordersMapper.updateOrdersById(orders);
        if (column < 1) {
            log.error("ID为{}的订单信息修改失败!返回值:{}", orders.getId(), column);
            return JsonRequest.error(ServiceEnum.UPDATE_ERROR);
        }
        log.info("ID为{}的订单信息修改成功!返回值:{}", orders.getId(), column);
        return JsonRequest.success(column);
    }

    /**
     * 删除订单信息
     *
     * @param ids ID值
     * @return JSON
     */
    @Override
    @CacheEvict(value = "order", allEntries = true)
    public JsonRequest<Integer> deleteOrder(Integer[] ids) {
        //判断订单信息是否完整
        Integer count = ordersMapper.countOrders(ids);
        if (count < ids.length) {
            log.warn("待删除的订单个数与数据库已存在的订单个数不符!数据库实际存在{}个,需要{}个", count, ids.length);
            return JsonRequest.error(ServiceEnum.NOT_FOUND);
        }
        //删除逻辑
        Integer column = ordersMapper.deleteOrdersById(ids);
        if (column < 1) {
            log.error("删除订单遇到错误!返回值{}", column);
            return JsonRequest.error(ServiceEnum.DELETE_ERROR);
        }
        log.info("已删除{}条订单信息!返回值{}", count, column);
        return JsonRequest.success(column);
    }
}
